import json
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.common.exceptions import NoSuchElementException
import random
import time

# 设置 ChromeDriver 路径
chrome_driver_path = "/usr/local/bin/chromedriver"


# 随机延时函数，用来模拟动作比较快的点击操作
def random_delay(time_start, time_end):
    delay = random.uniform(time_start, time_end)
    print("随机延时", delay)
    time.sleep(delay)


# 配置 Chrome 选项
options = Options()
options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(options=options)
driver.maximize_window()

driver.get("https://www.baidu.com")

wait=WebDriverWait(driver,2)

search_button=wait.until(EC.presence_of_element_located((By.ID, "kw")))
search_button.send_keys("java")


wait.until(EC.presence_of_element_located((By.ID, "su")))
search_button.submit()

# 等待搜索结果加载完成
wait.until(EC.presence_of_element_located((By.XPATH, '//div[@id="content_left"]/div/h3/a')))


results = driver.find_elements(By.XPATH, '//div[@id="content_left"]/div/h3/a')

for result in results:
    print(result.text)




tmp=driver.page_source
print(tmp)

time.sleep(10)
driver.quit()


